package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
@Api(tags = "套餐相关接口")
public class SetMealController {
    @Autowired
    private SetMealService setMealService;
    @GetMapping("/page")
    @ApiOperation("分页查询套餐")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO)
    {
        log.info("开始分页查询");
       PageResult pageResult=setMealService.page(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    @GetMapping("/{id}")
    @ApiOperation("查询指定套餐")
    public Result<SetmealVO> getById(@PathVariable Integer id)
    {
        log.info("通过id:{}查询套餐",id);
     SetmealVO setmealVO=setMealService.getByIdWithDishs(id);
     return Result.success(setmealVO);
    }
    @PutMapping
    @CacheEvict(cacheNames = "setMealCache",allEntries = true)
    @ApiOperation("修改套餐")
    public Result modify(@RequestBody SetmealDTO setmealDTO)
    {
        log.info("修改套餐:{}",setmealDTO);
        setMealService.update(setmealDTO);

        return Result.success(setmealDTO);
    }

    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setMealCache",allEntries = true)
    @ApiOperation("套餐起售，停售")
    public Result startWithEnd(@PathVariable Integer status,Integer id)
    {
        log.info("套餐状态改变");
        setMealService.startWithEnd(status,id);

        return Result.success();
    }

    @PostMapping
    @CacheEvict(cacheNames = "setMealCache",allEntries = true)
    @ApiOperation("新增套餐")
    public Result addMeal(@RequestBody SetmealDTO setmealDTO)
    {
        log.info("新增套餐:{}",setmealDTO);
        setMealService.addMeal(setmealDTO);

        return Result.success();
    }

    @DeleteMapping
    @CacheEvict(cacheNames = "setMealCache",allEntries = true)
    @ApiOperation("批量删除套餐")
    public Result delete(@RequestParam List<Integer> ids)
    {
        setMealService.deleteBatch(ids);

        return Result.success();
    }
}
